﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.IO.Compression;
using System.Xml;
using CustomControls;

namespace Pozr
{
    public partial class MainForm : Form
    {
        LogMessageDelegate logMessageDelegate;
        Timer startupTimer = new Timer();
        XTabControl tabControl;
        ControlSettings controlSettings;
        ControlPlugins controlPlugins;

        public MainForm()
        {
            startupTimer.Tick += new EventHandler(this.startupTimer_Tick);
            logMessageDelegate = new LogMessageDelegate(AddLogMessage);
            Middleware.WriteLog = new LogMessageDelegate(this.UpdateLog);
            InitializeComponent();

            startupTimer.Interval = 0;
            startupTimer.Enabled = true;
        }

        private void startupTimer_Tick(object sender, EventArgs e)
        {
            startupTimer.Enabled = false;

            Middleware.WriteLog("Starting up...");

            Middleware middleware = new Middleware(this);

            Middleware.WriteLog("Middleware initialized.");

            //Initialize the main interface
            tabControl = new XTabControl();
            controlSettings = new ControlSettings();
            controlPlugins = new ControlPlugins();
            controlSettings.InitSettings();
            Middleware.MapDisplay.Name = "Map";
            controlSettings.Name = "Settings";
            controlPlugins.Name = "Plugins";
            textBoxDebug.Name = "Log";
            tabControl.Add(Middleware.MapDisplay);
            tabControl.Add(controlSettings);
            tabControl.Add(controlPlugins);
            tabControl.Dock = DockStyle.Fill;
            this.Controls.Add(tabControl);

            Middleware.WriteLog("CLR Version: " + System.Environment.Version.ToString() + "\r\n");

            Middleware.PositionHandler.LoadModules();

            //Call the find plugins routine, to search in our Plugins Folder
            Middleware.Plugins.FindPlugins();

            //Add each plugin to the middleware interface
            foreach (Types.Plugin pluginOn in Middleware.Plugins.Plugins)
            {
                Middleware.WriteLog("Plugin loaded: " + pluginOn.Instance.Name);
                pluginOn.Instance.MainInterface.Name = pluginOn.Instance.Name;
                controlPlugins.AddPluginControl(pluginOn.Instance.MainInterface);
            }

            tabControl.Add(textBoxDebug);
            Middleware.WriteLog("Ready.");
        }

        public void UpdateLog(string s)
        {
            this.textBoxDebug.Invoke(logMessageDelegate, new object[] {s});
        }

        private void AddLogMessage(string message)
        {
            textBoxDebug.Text = textBoxDebug.Text.Insert(0, System.DateTime.Now.ToLongTimeString() + ": " + message + "\r\n");
        }

   }
}